package com.dd.shop4j.sys.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dd.shop4j.sys.mapper.SysCityMapper;
import com.dd.shop4j.sys.mapper.SysDistrictMapper;
import com.dd.shop4j.sys.mapper.SysProvinceMapper;
import com.dd.shop4j.sys.model.SysCity;
import com.dd.shop4j.sys.model.SysDistrict;
import com.dd.shop4j.sys.model.SysProvince;

/**
 * 省份，城市，区 信息Service类
 * 
 * @author 唐小恕
 * 
 */
@Service
public class AddressService {

	@Resource
	private SysProvinceMapper sysProvinceMapper;
	@Resource
	private SysCityMapper sysCityMapper;
	@Resource
	private SysDistrictMapper sysDistrictMapper;

	/**
	 * 根据查询类型和父节点的id查询地址信息
	 * 
	 * @param queryType 查询类型
	 * @param parentId  父节点id 省份查询时这个字段无意义
	 * @return 地址信息列表
	 */
	public List<AddressInfo> getAddress(QueryType queryType, Integer parentId) {
		List<AddressInfo> result = new ArrayList<AddressInfo>();
		if (QueryType.PROVINCE.equals(queryType)) {
			List<SysProvince> provinces = getAllProvinces();
			for (SysProvince province : provinces) {
				AddressInfo address = new AddressInfo();
				address.setCode(province.getId());
				address.setText(province.getName());
				result.add(address);
			}
		} else if (QueryType.CITY.equals(queryType)) {
			List<SysCity> citys = getCitysByProvinceId(parentId);
			for (SysCity city : citys) {
				AddressInfo address = new AddressInfo();
				address.setCode(city.getId());
				address.setText(city.getName());
				result.add(address);
			}
		} else if (QueryType.DISTRICT.equals(queryType)) {
			List<SysDistrict> districts = getDistrictsByCityId(parentId);
			for (SysDistrict district : districts) {
				AddressInfo address = new AddressInfo();
				address.setCode(district.getId());
				address.setText(district.getName());
				result.add(address);
			}
		}
		return result;
	}
	
	private List<SysDistrict> getDistrictsByCityId(Integer cityId) {
		SysDistrict district = new SysDistrict();
		if (cityId != null && cityId.intValue() > 0)
			district.setCityId(cityId);
		return sysDistrictMapper.queryBySelective(district);
	}

	private List<SysCity> getCitysByProvinceId(Integer provinceId) {
		SysCity city = new SysCity();
		if (provinceId != null && provinceId.intValue() > 0)
			city.setProvinceId(provinceId);
		return sysCityMapper.queryBySelective(city);
	}

	private List<SysProvince> getAllProvinces() {
		return sysProvinceMapper.queryBySelective(null);
	}

	/**
	 * 地区 查询类型
	 * 
	 * @author 唐小恕
	 * 
	 */
	public static enum QueryType {
		PROVINCE, CITY, DISTRICT;
	}

	/**
	 * 地址信息 内部类
	 * @author 唐小恕
	 *
	 */
	public static class AddressInfo implements Serializable {
		private static final long serialVersionUID = -9124291512153908835L;
		private Integer code;
		private String text;

		public Integer getCode() {
			return code;
		}

		public void setCode(Integer code) {
			this.code = code;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

	}

}
